<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js">&lt;?php
<span id='global-property-'>/**
</span> * Cube PHP Framework
 * 
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the &quot;License&quot;); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * @author Dillen / Steffen
 */

namespace Library\Mvc;
 
use \Library\Application;

<span id='Library-Mvc-Router'>/**
</span> * @class Library.Mvc.Router
 * 
 * Router class description
 */
class Router
{
<span id='Library-Mvc-Router-property-_application'>	/**
</span>	 * Application instance
	 * 
	 * @property {Library.Application} _application
	 * @private
	 */
	public $application;
	
<span id='Library-Mvc-Router-property-request'>	/**
</span>	 * Request
	 * 
	 * @property {String} request
	 */
	public $request;
	
<span id='Library-Mvc-Router-property-_rules'>	/**
</span>	 * Rewrite rules
	 * 
	 * @property {Array} _rules
	 * @private
	 */
	private $_rules = array();
	
<span id='Library-Mvc-Router-property-_controller'>	/**
</span>	 * Controller instance
	 * 
	 * @property {Library.Mvc.Controller} _controller
	 */
	private $_controller;
	
<span id='Library-Mvc-Router-method-__construct'>	/**
</span>	 * Constructor
	 * 
	 * @method __construct
	 * @param {Library.Mvc.Request} $request
	 * @param {String} $module
	 */
	public function __construct(Request $request)
	{
		$this-&gt;request = $request;
	}
	
<span id='Library-Mvc-Router-method-addRule'>	/**
</span>	 * Add router dispatch rule 
	 *
	 * @method addRule
	 * @param {String} $from
	 * @param {String} $to
	 */
	public function addRule($from, $to)
	{
		// TODO
	}
	
<span id='Library-Mvc-Router-method-setRequest'>	/**
</span>	 * Set the routers request object
	 * 
	 * @method setRequest
	 * @param {Library.Mvc.Request} $request
	 */
	public function setRequest(\Library\Mvc\Request $request)
	{
		$this-&gt;request = $request;
	}
	
<span id='Library-Mvc-Router-method-prepate'>	/**
</span>	 * Prepare router for dispatching
	 * 
	 * @method prepate
	 * @return {Library.Mvc.Controller} Controller instance
	 */
	public function prepare()
	{
		$result = $this-&gt;parseRouting();

		if ($result)
		{
			$this-&gt;setRequest(new Request($result));
		}
		
		try
		{
			$class = $this-&gt;application-&gt;config['application']['module'] . '\\Controllers\\' . $this-&gt;request-&gt;controller;
			$this-&gt;_controller = new $class;
		}
		catch(\Library\Autoloader\Exception\ClassNotFoundException $exception)
		{
			throw new \Exception('Controller ' . $this-&gt;request-&gt;controller . ' does not exist.');
		}
		
		// Inject instances into the controller
		$this-&gt;_controller-&gt;application = $this-&gt;application;
		$this-&gt;_controller-&gt;request = $this-&gt;request;
		$this-&gt;_controller-&gt;layout = new Layout($this-&gt;application-&gt;config);
		$this-&gt;_controller-&gt;layout-&gt;request = $this-&gt;request;
		
		$this-&gt;_controller-&gt;__init();
		
		return $this-&gt;_controller;
	}
	
<span id='Library-Mvc-Router-method-dispatch'>	/**
</span>	 * Dispatch the router and call the actual controller action
	 * 
	 * @method dispatch
	 */
	public function dispatch()
	{		
		if (!method_exists($this-&gt;_controller, $this-&gt;request-&gt;action . 'Action'))
		{
			throw new \Exception('Action ' . $this-&gt;request-&gt;action . ' does not exist.');
		}
		
		// If there are no parameters, call the controller action the normal way
		if (count($this-&gt;request-&gt;parameters) == 0)
		{
			$this-&gt;_controller-&gt;{$this-&gt;request-&gt;action . 'Action'}();
		}
		else
		{
			call_user_func_array(array($this-&gt;_controller, $this-&gt;request-&gt;action . 'Action'), $this-&gt;request-&gt;parameters);
		}
	}
	
<span id='Library-Mvc-Router-method-_parseRouting'>	/**
</span>	 * Parse routing
	 * 
	 * Checks in settings.ini for the routing section and parses all the routing configuration.
	 * If no routing is configured, the request won't change.
	 * 
	 * @method _parseRouting
	 * @private
	 * @return string|false Request uri string
	 */
	public function parseRouting()
	{
		// Get the application instance of this module
		$routing = $this-&gt;application-&gt;config['routing'];
		
		foreach ( $routing as $key =&gt; $value )
        {			
            // Convert wildcards
            $key = str_replace(':any', '(.+)', $key);
            
            if ( preg_match('#^' . $key . '$#', $this-&gt;request-&gt;requestUri) )
            {
                $value = rtrim($value);
                
                // Only replace back-references when there is one
                if ( strpos($value, '$') )
                {
                    $value = preg_replace('#^'.$key.'$#', $value, $this-&gt;request-&gt;requestUri);
                }
                
             	return $value;
            }
        }
		
		return false;
	}
}</pre>
</body>
</html>
